package com.webanovare.bb;

import com.webanovare.model.UserList;
import com.webanovare.model.UserRegistry;
import com.webanovare.model.db.User;
import com.webanovare.model.db.UserGroup;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.inject.Named;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

@Named("auth")
@SessionScoped
public class AuthBB {

    private static final Logger log = Logger.getLogger(AuthBB.class.getName());

    @EJB
    UserRegistry reg;
    
    public boolean addMe() {
        
        UserList.getInstance().addUser(getLoggedInAs(), new Date(System.currentTimeMillis()+120000));
        
        return true;
    }
    public boolean isAdmin() {
        FacesContext context = FacesContext.getCurrentInstance();
        HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();

        String userName = request.getRemoteUser();
        
        if (userName == null) {
            return false;
        }
        
        User u = reg.findUser(userName);
        
        if (u != null && u.getGroups().contains(UserGroup.ADMIN)) {
            return true;
        }
        return false;
    }
    public List<String> getUsers() {
        
        return UserList.getInstance().getUsers();
    }
    
    public String getLoggedInAs() {
        FacesContext context = FacesContext.getCurrentInstance();
        HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();

        String userName = request.getRemoteUser();
        
        if (userName == null) {
            userName = "Guest-" + hashGuest();
        }
        return userName;
    }
    public String loginRedirect() {
        String result = "/users?faces-redirect=true";

        FacesContext context = FacesContext.getCurrentInstance();
        HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();

        String userName = request.getRemoteUser();
        
        if (reg.isInAdminGroup(userName)) {
            result = "jsf/adminsFace?faces-redirect=true";
        }
        
        return result;
    }
    public String logout() {
        String result = "/index?faces-redirect=true";

        FacesContext context = FacesContext.getCurrentInstance();
        HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();

        
        try {
            request.logout();
        } catch (ServletException e) {
            log.log(Level.SEVERE, "Failed to logout user!", e);
            result = "/loginError?faces-redirect=true";
        }

        return result;
    }
    public boolean isLoggedIn() {
        FacesContext context = FacesContext.getCurrentInstance();
        HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();

        String userName = request.getRemoteUser();
        
        return userName != null;
    }
    public boolean isNotLoggedIn() {
        return !isLoggedIn();
    }
    
    public int hashGuest() {
        int hash = 3;
        FacesContext context = FacesContext.getCurrentInstance();
        HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();

        String userName = request.getRemoteUser();
        
        
        for (byte b : request.getSession().getId().getBytes()) {
            hash = 11 * hash + b;
        }
        
        return hash;
    }
}
